<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Blog extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();

        $this->load->helper(array('form','url'));
        $this->load->library(array('session'));
        $this->load->database();

        //加载模块
        $this->load->model('type_model');
        $this->load->model('blog_model');

        //调试模式
        //$this->output->enable_profiler(TRUE);
    }

    /****************** 页面显示 ***********************
     * index => 博客表
     * add => 添加博客
     * edit => 编辑博客
     */

    public function index()
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        //定义网页相关数据
        $data['title'] = 'Li Lijie - 博客管理';
        $data['list'] = $this->blog_model->getlist();
        foreach ($data['list'] as $key => $v){
            $type = $this->type_model->get($v['tid']);
            $data['list'][$key]['type'] = $type['name'];
        }

        //载入
        $this->load->view('admin/header',$data);
        $this->load->view('admin/bloglist');
        $this->load->view('admin/footer');
    }

    public function add()
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        //定义网页相关数据
        $data['title'] = 'Li Lijie - 添加博客';
        $data['list'] = $this->type_model->getlist();

        //载入页面
        $this->load->view('admin/header',$data);
        $this->load->view('admin/blogadd');
        $this->load->view('admin/footer');
    }

    public function edit($id = '')
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        //定义网页相关数据
        $data['title'] = 'Li Lijie - 编辑博客';
        $data['list'] = $this->type_model->getlist();
        $data['blog'] = $this->blog_model->get($id);

        //载入页面
        $this->load->view('admin/header',$data);
        $this->load->view('admin/blogedit');
        $this->load->view('admin/footer');
    }

    /****************** 相关操作 ***********************
     * addblog => 添加
     * editblog => 编辑
     * delblog => 删除
     */

    public function addblog()
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        if(!isset($_POST))
            jump('/admin/editblog','表单为空');

        //获取表单信息
        $data = array(
            'title' => $this->input->post('title'),  //标题
            'intro' => $this->input->post('intro'),  //简介
            'content' => $this->input->post('html'),  //正文
            'tid' => $this->input->post('type'),  //分类
            'date' => date("Y-m-d H:i:s"),  //发表日期
        );

        //数据库操作
        $this->blog_model->add($data);
        jump('/admin/blog','添加成功！');  //跳转
    }

    public function editblog()
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        if(!isset($_POST))
            jump('/admin/editblog','表单为空');

        //获取表单信息
        $data = array(
            'title' => $this->input->post('title'),  //标题
            'intro' => $this->input->post('intro'),  //简介
            'content' => $this->input->post('html'),  //正文
            'tid' => $this->input->post('type'),  //分类
        );

        //数据库操作
        $id = $this->input->post('id');
        $this->blog_model->update($id,$data);  //数据库操作
        jump('/admin/blog','修改成功！');  //跳转
    }

    public function delblog($ids = '')
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        $id= explode('_',$ids);
        for($i = 0; $i < count($id); $i++)
            $this->blog_model->del($id[$i]);
        jump('/admin/blog','删除成功！');  //跳转
    }

    public function search()
    {
        if(islogin() == false){jump('/admin/login','请先登录');return;}

        //定义网页相关数据
        $data['title'] = 'Li Lijie - 博客管理';
        $data['list'] = $this->blog_model->search($_GET["word"]);
        foreach ($data['list'] as $key => $v){
            $type = $this->type_model->get($v['tid']);
            $data['list'][$key]['type'] = $type['name'];
        }

        //载入
        $this->load->view('admin/header',$data);
        $this->load->view('admin/bloglist');
        $this->load->view('admin/footer');
    }
}
